<template>
	<view>
		<form @submit="formSubmit">
			<view class="uni-form-item item_horizon">
				<view class="title">名称</view>
				<input type="text" placeholder="请输入名称..." v-model="addForm.food.name">
			</view>
			<view class="uni-form-item item_horizon">
				<view class="title">价格</view>
				<input type="number" placeholder="请输入价格..." v-model="addForm.food.price">
			</view>
			<view class="uni-form-item item_horizon">
				<view class="title">地址</view> 
				<input type="text" placeholder="请选择地址..." @focus="chooseLocation" v-model="address.address">
			</view>
			<view class="uni-form-item item_horizon">
				<view class="title">推荐指数</view>
				<slider value="100" @change="sliderChange" min="1" max="5" show-value activeColor="#F0AD4E" />
			</view>
			
			<view class="uni-form-item ">
				<view class="title">图片</view>
				<view class="img-field">
					<image class="img-add" v-for="item in imagepaths" v-if="imagepaths" :src='item'></image>
					<view class="img-add" @click="addImage">
					<text>+</text>
					</view>
				</view>
			</view>
			<view class="uni-form-item ">
				<view class="title">分享理由</view>
				<textarea  placeholder="请输入分享理由..." auto-height v-model="addForm.food.reason"/>
			</view>
			<view class="uni-form-item ">
				<view class="title">标签</view>
				<checkbox-group @change="checkboxChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="item in tags" :key="item.value">
					    <view>
					        <checkbox :value="item.value" :checked="item.checked" />
					    </view>
					    <view>{{item.name}}</view>
					</label>
				</checkbox-group>
			</view>
			<view class="space">
				
			</view>
			<button form-type="submit" >提交</button>
		</form>
	</view>
</template>
	
<script>
	export default {
		data() {
			return {
				addForm:{
					article:{
						pictures:[
							{
							filePath:""
							},
							{
							filePath:""
						}]
					},
					food:{
						name:"",
						index:"",
						reason:"",
						price:"",
						addressY:0,
						addressX:0,
						labelObjects:[
							{
								label:""
							}
						]
						
					},
					userID:10001
				},
				imagepaths:[],
				address:'',
				tags:[{
					value:1,
					name:"特色小吃"
				},
				{
					value:2,
					name:"异域美食"
				},
				{
					value:3,
					name:"甜点饮品"
				},
				{
					value:4,
					name:"能量早餐"
				},
				{
					value:5,
					name:"正餐优选"
				},
				{
					value:6,
					name:"夜宵必备"
				}]
			}
		},
		methods: {
			sliderChange(e){
				this.addForm.food.index=e.detail.value
			},
			formSubmit(){
				var that=this
				uni.showModal({
				    title: '提示',
				    content: '是否确认发布？',
				    success: function (res) {
				        if (res.confirm) {
				           that.Submit()
						   uni.switchTab({
						   	url:'/pages/index/index'
						   })
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			async Submit(){
				var that=this
				this.addForm.food.addressY=this.address.latitude
				this.addForm.food.addressX=this.address.longitude
				const res=await this.$myDoPost({
					url:'/FoodArticle/api/addFoodArticle',
					data:that.addForm
				})
			},
			checkboxChange(e){
				this.addForm.food.labelObjects=[]
				var that=this
				e.detail.value.forEach(item=>{
					that.addForm.food.labelObjects.push({
						label:item
					})
				})
			},
			chooseLocation(){
				var that=this;
				uni.chooseLocation({
					latitude: 30.526230995480322,
					longitude: 114.35812750362396,
				    success: function (res) {
				        console.log('位置名称：' + res.name);
				        that.address=res
				        console.log('纬度：' + res.latitude);
				        console.log('经度：' + res.longitude);
				    }
				});
			},
			addImage() {
				var that=this;
			    uni.chooseImage({
			        count: 3, 
			        sourceType: ['album'], //从相册选择
			        success: (res) => {			
			           res.tempFilePaths.forEach(item=>
					   {
						   that.imagepaths.push(item)
					   })
						
			        }
			    });
			}
		}
	}
</script>

<style lang="less">
	button{
		background-color:#F0AD4E;
		color: #FFFFFF;
	}
	page{
		padding-top:50rpx;
	}
	.item_horizon{
			display: flex;
	}
	.item_img{
		margin-top: 100rpx;
	}
	.space{
		width:100%;
		height: 70px;
	}
.uni-form-item{
	padding-left: 40rpx;
	padding-right: 40rpx;
	justify-content: space-between;
	align-items: center;
	margin-bottom:30rpx ;
	input{
	height: 80rpx;
	padding-left: 20rpx;
	padding-right: 20rpx;
	background: #fff;
	width:80%;
	border: 1px solid #f8f8f8;
	border-radius: 10rpx;
	}
	textarea{
		background: #fff;
		width:100%;
		min-height: 200rpx;
		border: 1px solid #f8f8f8;
		border-radius: 10rpx;
		float: right;
		margin-top: 20rpx;
		margin-bottom: 15rpx;
		
	}
	slider{
		width:70%;
	}
	checkbox-group{
		
		.uni-list-cell {
		    display: flex;
			width:33%;
			float:left;
			margin-top:10rpx;
		}

	}
	.img-field{
		display: flex;
		margin-top:20rpx;
		.img-add{
			width:180rpx;
			height:200rpx;
			text-align: center;
			background-color:#C0C0C0;
			margin-right: 10rpx;
			text{
				font-size: 50px;
				line-height: 100px;
			}
		}
	}
}

</style>
